//
//  SendDetailViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/8/10.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "SendDetailViewController.h"
#import "Base.h"
#import "ItemsTableViewCell.h"
#import "OrderMessagesignle.h"
#import "DetailViewController.h"
#import "Order.h"
#import "Consignee.h"
@interface SendDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *receiveArray;
    NSMutableArray *noreceiveArray;
    int row;
}

@property(nonatomic,strong)UIButton *receiveButtom;
@property(nonatomic,strong)UIButton *noreceiveButton;
@property(nonatomic,strong)UILabel *receiveLab;
@property(nonatomic,strong)UILabel *noreceiveLab;
@property(nonatomic,strong)UITableView *mainTable;
@end

@implementation SendDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    row=0;
    [self setBack];
    self.view.backgroundColor=COLOR;
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 45)];
    view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:view];
    
    self.receiveButtom=[[UIButton alloc] initWithFrame:CGRectMake(1, 0, WIDTH/2-1, 40)];
    self.noreceiveButton=[[UIButton alloc] initWithFrame:CGRectMake(WIDTH/2, 0, WIDTH/2-1, 40)];
    
    self.receiveLab=[[UILabel alloc] initWithFrame:CGRectMake(10, 42, WIDTH/2-20, 2)];
    self.noreceiveLab=[[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2+10, 42, WIDTH/2-20, 2)];
    self.receiveLab.backgroundColor=BLUECOLOL;
    self.noreceiveLab.backgroundColor=[UIColor whiteColor];
    
    [view addSubview:self.receiveLab];
    [view addSubview:self.noreceiveLab];
    
    [self.receiveButtom setTitle:@"已签收" forState:UIControlStateNormal];
    [self.noreceiveButton setTitle:@"未签收" forState:UIControlStateNormal];
    [view addSubview:self.receiveButtom];
    [view addSubview:self.noreceiveButton];
    self.receiveButtom.backgroundColor=[UIColor whiteColor];
    [self.receiveButtom setTitleColor:BLUECOLOL forState:UIControlStateNormal];
    self.noreceiveButton.backgroundColor=[UIColor whiteColor];
    [self.noreceiveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    
    [self.receiveButtom addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.noreceiveButton addTarget:self action:@selector(turnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.mainTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 115, WIDTH, HEIGHT-115)];
    self.mainTable.delegate=self;
    self.mainTable.dataSource=self;
    self.mainTable.backgroundColor=COLOR;
    self.mainTable.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.mainTable];   
    [self shuju];
        // Do any additional setup after loading the view.
}
-(void)shuju
{
    NSMutableArray *array=[NSMutableArray array];
    NSMutableArray *arrayq=[NSMutableArray array];
    for (int i=0; i<self.delivery.orders.count; i++)
        {
        Order *order=[[Order alloc] init];
        [order setValuesForKeysWithDictionary:self.delivery.orders[i]];
       
        if (order.traceStatus.integerValue==0)
            {
            [array addObject:order];
            }
       
        if (order.traceStatus.integerValue!=0)
            {
            [arrayq addObject:order];
            }
  
        }
    receiveArray=[NSMutableArray arrayWithArray:array];
    noreceiveArray=[NSMutableArray arrayWithArray:arrayq];
        // NSLog(@"%d %d",receiveArray.count,noreceiveArray.count);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (row==0)
        {
        if (receiveArray.count==0)
            {
            return 0;
         }
        else
            {
        return receiveArray.count;
            }
       }
    else        
        {
        if (noreceiveArray.count==0)
            {
            return 0;
          }
        else
            {
        return noreceiveArray.count;
            }
        }
}

   
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid=@"itemcellid";
    ItemsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell==nil)
        {
        cell=[[ItemsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    Order *order;
    
    if (row==0)
        {
        if (receiveArray.count==0)
            {
            UITableViewCell *cell=[[UITableViewCell alloc] init];
            return cell;
          }
        else
            {
        order=receiveArray[indexPath.row];
            }
        }
    else 
        {
        if (noreceiveArray.count==0)
            {
            UITableViewCell *cell=[[UITableViewCell alloc] init];
            return cell;
           }
        else
            {
        order=noreceiveArray[indexPath.row];
            }
        }
    NSString *s;
    if (order.howToPay.integerValue==0)
        {
        s=@"货到付款";
        }
    else
        {
        s=@"已付款";
        }
    cell.oidLaber.text=[NSString stringWithFormat:@"%@",[order.oid substringFromIndex:order.oid.length-3]];
    cell.oidLaber.font=[UIFont boldSystemFontOfSize:20];
    cell.oidLaber.textColor=[UIColor redColor];
    cell.payLaber.text=[NSString stringWithFormat:@"实付款%.2lf %@",order.totalCast.doubleValue,s];
    Consignee *consignee=[[Consignee alloc] init];
    [consignee setValuesForKeysWithDictionary:order.consignee];
    cell.addressLaber.text=[NSString stringWithFormat:@"地址: %@ %@  %@",consignee.region,consignee.bname,consignee.address];
    cell.phoneLaber.text=consignee.cellphone;
    
    
    NSString *str=[NSString stringWithFormat:@"%lld",self.delivery.sentTime.longLongValue/1000];//开始时间戳
    NSTimeInterval time=[str doubleValue];//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    cell.beginLaber.text=[NSString stringWithFormat:@"发货时间:%@",currentDateStr];
    cell.typeLaber.text=[self setOrderType:order.traceStatus.integerValue];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
       
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return 190;
}
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    Order *order;
    if (row==0)
        {
        order=receiveArray[indexPath.row];
        }
    else
        {
        order=noreceiveArray[indexPath.row];
        }
    
    
    [OrderMessagesignle single].oidString=order._id;
    [[OrderMessagesignle single] orderMessagworking];
    DetailViewController *detailViewController=[[DetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailViewController animated:YES];

}
-(void)turnAction:(UIButton *)sender
{
    if ([sender.titleLabel.text isEqualToString:@"已签收"])
        {
        self.receiveLab.backgroundColor=BLUECOLOL;
        self.noreceiveLab.backgroundColor=[UIColor whiteColor];
//        self.receiveButtom.backgroundColor=[UIColor blueColor];
        [self.receiveButtom setTitleColor:BLUECOLOL forState:UIControlStateNormal];
//        self.noreceiveButton.backgroundColor=[UIColor whiteColor];
        [self.noreceiveButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        row=0;
        [self.mainTable reloadData];
        
      }
    else
        {
        self.receiveLab.backgroundColor=[UIColor whiteColor];
        self.noreceiveLab.backgroundColor=BLUECOLOL;
//        self.noreceiveButton.backgroundColor=[UIColor blueColor];
        [self.noreceiveButton setTitleColor:BLUECOLOL forState:UIControlStateNormal];
//        self.receiveButtom.backgroundColor=[UIColor whiteColor];
        [self.receiveButtom setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        row=1;

        [self.mainTable reloadData];
                }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
